package com.edu.study.base.stuQuestionCatalog.service.impl;

import com.edu.study.base.stuQuestionCatalog.domain.StuQuestionCatalog;
import com.edu.study.base.stuQuestionCatalog.mapper.StuQuestionCatalogMapper;
import com.edu.study.base.stuQuestionCatalog.service.IStuQuestionCatalogService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 题卷目录Service业务层处理
 * 
 * @author jjt
 * @date 2021-05-18
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuQuestionCatalogServiceImpl implements IStuQuestionCatalogService 
{
    @Autowired
    private StuQuestionCatalogMapper stuQuestionCatalogMapper;

    /**
     * 查询题卷目录
     * 
     * @param id 题卷目录ID
     * @return 题卷目录
     */
    @Override
    public StuQuestionCatalog selectStuQuestionCatalogById(Long id)
    {
        return stuQuestionCatalogMapper.selectStuQuestionCatalogById(id);
    }

    @Override
    public StuQuestionCatalog selectTreeDataByName(String name) {
        StuQuestionCatalog stuQuestionCatalog = stuQuestionCatalogMapper.selectStuQuestionCatalogByName(name);
        stuQuestionCatalog.setChildren(dgSelectChildren(stuQuestionCatalog));
        return stuQuestionCatalog;
    }

    @Override
    public List<StuQuestionCatalog> selectByCatalogIds(List<Long> catalogIds) {
        return stuQuestionCatalogMapper.selectByCatalogIds(catalogIds);
    }

    @Override
    public StuQuestionCatalog selectTreeDataById(Long id) {
        StuQuestionCatalog stuQuestionCatalog = stuQuestionCatalogMapper.selectStuQuestionCatalogById(id);
        stuQuestionCatalog.setChildren(dgSelectChildren(stuQuestionCatalog));
        return stuQuestionCatalog;
    }


    //递归查询所有的下级
    public List<StuQuestionCatalog>  dgSelectChildren(StuQuestionCatalog stuQuestionCatalog){
        StuQuestionCatalog catalog = new StuQuestionCatalog();
        catalog.setParentId(stuQuestionCatalog.getId());
        List<StuQuestionCatalog> questionCatalogList = stuQuestionCatalogMapper.selectStuQuestionCatalogList(catalog);
        if(questionCatalogList != null && questionCatalogList.size() > 0){
            for (StuQuestionCatalog questionCatalog : questionCatalogList) {
                questionCatalog.setChildren(dgSelectChildren(questionCatalog));
            }
        }
        return questionCatalogList;
    }






    /**
     * 查询题卷目录列表
     * 
     * @param stuQuestionCatalog 题卷目录
     * @return 题卷目录
     */
    @Override
    public List<StuQuestionCatalog> selectStuQuestionCatalogList(StuQuestionCatalog stuQuestionCatalog)
    {
        return stuQuestionCatalogMapper.selectStuQuestionCatalogList(stuQuestionCatalog);
    }

    @Override
    public List<StuQuestionCatalog> selectQuestionCatalogList(StuQuestionCatalog stuQuestionCatalog) {
        return stuQuestionCatalogMapper.selectQuestionCatalogList(stuQuestionCatalog);
    }

    /**
     * 新增题卷目录
     * 
     * @param stuQuestionCatalog 题卷目录
     * @return 结果
     */
    @Override
    public int insertStuQuestionCatalog(StuQuestionCatalog stuQuestionCatalog)
    {
        stuQuestionCatalog.setCreateTime(DateUtils.getNowDate());
        return stuQuestionCatalogMapper.insertStuQuestionCatalog(stuQuestionCatalog);
    }

    /**
     * 修改题卷目录
     * 
     * @param stuQuestionCatalog 题卷目录
     * @return 结果
     */
    @Override
    public int updateStuQuestionCatalog(StuQuestionCatalog stuQuestionCatalog)
    {
        stuQuestionCatalog.setUpdateTime(DateUtils.getNowDate());
        return stuQuestionCatalogMapper.updateStuQuestionCatalog(stuQuestionCatalog);
    }

    /**
     * 批量删除题卷目录
     * 
     * @param ids 需要删除的题卷目录ID
     * @return 结果
     */
    @Override
    public int deleteStuQuestionCatalogByIds(Long[] ids)
    {
        return stuQuestionCatalogMapper.deleteStuQuestionCatalogByIds(ids);
    }

    /**
     * 删除题卷目录信息
     * 
     * @param id 题卷目录ID
     * @return 结果
     */
    @Override
    public int deleteStuQuestionCatalogById(Long id)
    {
        return stuQuestionCatalogMapper.deleteStuQuestionCatalogById(id);
    }


}
